﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormLayFireExtinguisher : System.Windows.Forms.Form
	{
		public FormLayFireExtinguisher(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.initParameterSetting();
		}

		public FormLayFireExtinguisher(Autodesk.Revit.DB.Document doc, FireExtinguisherParam fp)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.initParameterSettingByChooseDlg(fp);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.GetFireExtinguisherPara())
			{
				base.DialogResult = DialogResult.OK;
			}
		}

		private void FormLayFireExtinguisher_Load(object sender, EventArgs e)
		{
			this.GetfamilyNameFromData(this.familyCategory);
			if (this.listname.Count > 0)
			{
				foreach (ListItem listItem in this.listname)
				{
					if (listItem.Name == this.name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
			this.cbcount.Value = this.fireExtinguisherParam.Count;
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void checkBoxbz_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void rbkx_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbkx.Checked)
			{
				this.layTerminalType = 0;
			}
		}

		private void rbry_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbry.Checked)
			{
				this.layTerminalType = 1;
			}
		}

		private void rbShouti_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbshouti.Checked)
			{
				this.familyCategory = 0;
				this.GetModeAgentByStyle(this.familyCategory, "手提式");
			}
		}

		private void rbTuiche_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtuiche.Checked)
			{
				this.familyCategory = 1;
				this.GetModeAgentByStyle(this.familyCategory, "推车式");
			}
		}

		private void picbox_Click(object sender, EventArgs e)
		{
			this.name = this.lbfname.Text;
			FormChooseHydrant formChooseHydrant = new FormChooseHydrant(this.name, this.listInfos);
			formChooseHydrant.Text = "选择灭火器";
			formChooseHydrant.Owner = this;
			if (formChooseHydrant.ShowDialog() == DialogResult.OK && this.name != formChooseHydrant.ImageName && formChooseHydrant.ImageName != "")
			{
				this.name = formChooseHydrant.ImageName;
				this.lbfname.Text = this.name;
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
		}

		private void lbfname_TextChanged(object sender, EventArgs e)
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			this.id = 0;
			if (this.listname.Count > 0)
			{
				foreach (ListItem listItem in this.listname)
				{
					if (this.lbfname.Text.Trim() == listItem.Name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
					}
				}
			}
			foreach (ListItem listItem2 in spaceFamilyDataReader.GetInfoById("FireExtinguisher", this.id))
			{
				if (listItem2.Name == "Image")
				{
					if (File.Exists(listItem2.Value))
					{
						this.picbox.Image = new Bitmap(listItem2.Value);
					}
					else
					{
						this.picbox.Image = null;
					}
				}
				else if (listItem2.Name == "RadiuDiameter")
				{
					this.tbradiu.Text = listItem2.Value;
				}
				else if (listItem2.Name == "Scale")
				{
					this.tbscale.Text = listItem2.Value;
				}
			}
		}

		private void cbcategory_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cbclass.Items.Clear();
			foreach (FireExtinguisherClass fireExtinguisherClass in this.listfe)
			{
				if (this.cbcategory.SelectedItem.ToString().Contains(fireExtinguisherClass.Model))
				{
					if (fireExtinguisherClass.LevelA != "" && fireExtinguisherClass.LevelA != null)
					{
						this.cbclass.Items.Add(fireExtinguisherClass.LevelA);
					}
					if (fireExtinguisherClass.LevelB != "" && fireExtinguisherClass.LevelB != null)
					{
						this.cbclass.Items.Add(fireExtinguisherClass.LevelB);
					}
				}
			}
			if (this.cbclass.Items.Count > 1)
			{
				if (this.isReadConfig)
				{
					this.cbclass.SelectedIndex = int.Parse(this.GetValueFromDic("class", "0", this.dic));
					return;
				}
				foreach (object obj in this.cbclass.Items)
				{
					if (obj.ToString().Trim() == this.fireExtinguisherParam.JiBie.Trim())
					{
						this.cbclass.SelectedItem = obj;
						break;
					}
				}
				if (this.cbclass.SelectedItem == null)
				{
					this.cbclass.SelectedIndex = 0;
					return;
				}
			}
			else if (this.cbclass.Items.Count == 1)
			{
				this.cbclass.SelectedIndex = 0;
			}
		}

		private void FormLayFireExtinguisher_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				this.UserSettingWrite();
			}
		}

		public void GetModeAgentByStyle(int fc, string style)
		{
			this.familyCategory = fc;
			this.GetfamilyNameFromData(fc);
			if (this.listname.Count > 0)
			{
				string a = "";
				using (List<ListItem>.Enumerator enumerator = this.listname.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.Name == this.familyName)
						{
							a = this.familyName;
							break;
						}
					}
				}
				if (a == "")
				{
					this.name = this.listname[0].Name;
					this.lbfname.Text = this.listname[0].Name;
				}
				else
				{
					this.lbfname.Text = this.familyName;
				}
			}
			this.listfe = this.sfdr.GetFireExtinguisherData("FireExtinguisherData", style);
			if (this.listfe.Count > 0)
			{
				this.cbcategory.Items.Clear();
				foreach (FireExtinguisherClass fireExtinguisherClass in this.listfe)
				{
					this.cbcategory.Items.Add(fireExtinguisherClass.Model + "(" + fireExtinguisherClass.Agent + ")");
				}
				if (this.isReadConfig)
				{
					if (this.cbcategory.Items.Count > this.category)
					{
						this.cbcategory.SelectedIndex = this.category;
						return;
					}
					this.cbcategory.SelectedIndex = 0;
					return;
				}
				else
				{
					foreach (object obj in this.cbcategory.Items)
					{
						if (obj.ToString().Trim() == this.fireExtinguisherParam.XingHao.Trim())
						{
							this.cbcategory.SelectedItem = obj;
							break;
						}
					}
					if (this.cbcategory.SelectedItem == null)
					{
						this.cbcategory.SelectedIndex = 0;
					}
				}
			}
		}

		private void GetfamilyNameFromData(int familyCategory)
		{
			this.listname = this.sfdr.GetAllName("FireExtinguisher", familyCategory);
			this.listInfos = this.sfdr.GetAllNameAndImage("FireExtinguisher", familyCategory);
		}

		public string GetValueFromDic(string name, string defaultvalue, Dictionary<string, string> dic)
		{
			if (dic.FirstOrDefault((KeyValuePair<string, string> q) => q.Key == name).Value.ToString() == "")
			{
				return defaultvalue;
			}
			return dic.FirstOrDefault((KeyValuePair<string, string> q) => q.Key == name).Value.ToString();
		}

		private void initParameterSetting()
		{
			this.dic = this.userSetting.GetFireExtinguisherLaySetting();
			this.isReadConfig = true;
			this.fireExtinguisherParam = new FireExtinguisherParam();
			this.fireExtinguisherParam.JiBie = this.GetValueFromDic("class", "0", this.dic);
			this.fireExtinguisherParam.Radius = (double)int.Parse(this.GetValueFromDic("radius", "20", this.dic));
			this.fireExtinguisherParam.Scale = double.Parse(this.GetValueFromDic("scale", "1", this.dic));
			this.fireExtinguisherParam.OffSet = double.Parse(this.GetValueFromDic("offset", "0", this.dic));
			this.fireExtinguisherParam.Count = int.Parse(this.GetValueFromDic("count", "2", this.dic));
			this.familyCategory = int.Parse(this.GetValueFromDic("familyCategory", "0", this.dic));
			this.familyName = this.GetValueFromDic("familyName", "清水灭火器", this.dic);
			this.category = int.Parse(this.GetValueFromDic("category", "0", this.dic));
			this.layTerminalType = int.Parse(this.GetValueFromDic("layTerminalType", "0", this.dic));
			if (this.familyCategory == 0)
			{
				this.rbshouti.Checked = true;
			}
			else if (this.familyCategory == 1)
			{
				this.rbtuiche.Checked = true;
			}
			this.tbradiu.Text = this.fireExtinguisherParam.Radius.ToString();
			this.tbscale.Text = this.fireExtinguisherParam.Scale.ToString();
			this.tbLevel.Text = this.fireExtinguisherParam.OffSet.ToString();
			try
			{
				this.checkBoxbz.Checked = Convert.ToBoolean(this.GetValueFromDic("islable", "True", this.dic));
				this.chkRange.Checked = Convert.ToBoolean(this.GetValueFromDic("range", "True", this.dic));
			}
			catch (Exception)
			{
			}
			if (this.layTerminalType == 0)
			{
				this.rbkx.Checked = true;
				return;
			}
			this.rbry.Checked = true;
		}

		private void initParameterSettingByChooseDlg(FireExtinguisherParam fp)
		{
			this.isReadConfig = false;
			this.dic = this.userSetting.GetFireExtinguisherLaySetting();
			this.fireExtinguisherParam = new FireExtinguisherParam();
			this.fireExtinguisherParam = fp;
			if (this.fireExtinguisherParam.SymbolName == "手提式")
			{
				this.familyCategory = 0;
				this.rbshouti.Checked = true;
			}
			else if (this.fireExtinguisherParam.SymbolName == "推车式")
			{
				this.familyCategory = 1;
				this.rbtuiche.Checked = true;
			}
			this.layTerminalType = int.Parse(this.GetValueFromDic("layTerminalType", "0", this.dic));
			this.fireExtinguisherParam.Radius = (double)int.Parse(this.GetValueFromDic("radius", "20", this.dic));
			this.fireExtinguisherParam.Scale = double.Parse(this.GetValueFromDic("scale", "1", this.dic));
			this.fireExtinguisherParam.OffSet = double.Parse(this.GetValueFromDic("offset", "0", this.dic));
			this.tbradiu.Text = this.fireExtinguisherParam.Radius.ToString();
			this.tbscale.Text = this.fireExtinguisherParam.Scale.ToString();
			this.tbLevel.Text = this.fireExtinguisherParam.OffSet.ToString();
			try
			{
				this.checkBoxbz.Checked = Convert.ToBoolean(this.GetValueFromDic("islable", "True", this.dic));
				this.chkRange.Checked = Convert.ToBoolean(this.GetValueFromDic("range", "True", this.dic));
			}
			catch (Exception)
			{
			}
			if (this.layTerminalType == 0)
			{
				this.rbkx.Checked = true;
				return;
			}
			this.rbry.Checked = true;
		}

		private void UserSettingWrite()
		{
			this.tbradiu.Text = StringTools.RemoveZero(this.tbradiu.Text.Trim());
			this.tbscale.Text = StringTools.RemoveZero(this.tbscale.Text.Trim());
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("familyCategory", this.familyCategory.ToString());
				dictionary.Add("familyName", this.lbfname.Text.Trim());
				dictionary.Add("layTerminalType", this.layTerminalType.ToString());
				dictionary.Add("islable", this.checkBoxbz.Checked.ToString());
				dictionary.Add("range", this.chkRange.Checked.ToString());
				dictionary.Add("radius", this.tbradiu.Text);
				dictionary.Add("category", this.cbcategory.SelectedIndex.ToString());
				dictionary.Add("class", this.cbclass.SelectedIndex.ToString());
				dictionary.Add("count", this.cbcount.Value.ToString());
				dictionary.Add("scale", this.tbscale.Text);
				dictionary.Add("offset", this.tbLevel.Text);
				this.userSetting.WriteFireExtinguisherLaySetting(dictionary);
				this.SaveParasFofDB();
			}
			catch (Exception)
			{
			}
		}

		private void SaveParasFofDB()
		{
			List<ListItem> list = new List<ListItem>();
			list.Add(new ListItem("RadiuDiameter", this.fireExtinguisherParam.Radius.ToString()));
			list.Add(new ListItem("Scale", this.fireExtinguisherParam.Scale.ToString()));
			if (!this.sfdr.SetInfoById("FireExtinguisher", this.fireExtinguisherParam.Id, list))
			{
				YJKMessageBox.Show("保存数据失败");
			}
		}

		private bool GetFireExtinguisherPara()
		{
			if (!(this.tbscale.Text.Trim() != "") || !(this.tbradiu.Text.Trim() != "") || !(this.tbLevel.Text.Trim() != "") || !(this.cbclass.SelectedItem.ToString() != "") || !(this.cbcategory.SelectedItem.ToString() != "") || !(this.cbcount.Value.ToString() != "") || this.id <= 0)
			{
				YJKMessageBox.Show("参数都不能为空");
				return false;
			}
			if (double.Parse(this.tbradiu.Text.Trim()) == 0.0)
			{
				YJKMessageBox.Show("保护半径不能为0");
				return false;
			}
			if (double.Parse(this.tbscale.Text.Trim()) < 0.5)
			{
				YJKMessageBox.Show("缩放比例不能小于0.5");
				return false;
			}
			this.fireExtinguisherParam = new FireExtinguisherParam();
			this.fireExtinguisherParam.Scale = double.Parse(this.tbscale.Text.Trim());
			this.fireExtinguisherParam.Radius = double.Parse(this.tbradiu.Text.Trim());
			this.fireExtinguisherParam.OffSet = double.Parse(this.tbLevel.Text.Trim());
			this.fireExtinguisherParam.JiBie = this.cbclass.SelectedItem.ToString();
			this.fireExtinguisherParam.XingHao = this.cbcategory.SelectedItem.ToString();
			foreach (FireExtinguisherClass fireExtinguisherClass in this.listfe)
			{
				if (fireExtinguisherClass.Model + "(" + fireExtinguisherClass.Agent + ")" == this.fireExtinguisherParam.XingHao.Trim())
				{
					this.fireExtinguisherParam.Mode = fireExtinguisherClass.Model;
					this.fireExtinguisherParam.Agent = fireExtinguisherClass.Agent;
					this.fireExtinguisherParam.SymbolName = fireExtinguisherClass.Style;
				}
			}
			this.fireExtinguisherParam.IsLable = this.checkBoxbz.Checked;
			this.fireExtinguisherParam.Range = this.chkRange.Checked;
			this.fireExtinguisherParam.Count = int.Parse(this.cbcount.Value.ToString());
			this.fireExtinguisherParam.Id = this.id;
			return true;
		}

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private List<ListItem> listInfos = new List<ListItem>();

		private HVACUserSetting userSetting = new HVACUserSetting();

		public FireExtinguisherParam fireExtinguisherParam;

		private string name = "";

		public int id;

		public FamilySymbol fs;

		private string familyName = "";

		public int familyCategory = 1;

		public int layTerminalType;

		public int category;

		private List<ListItem> listname = new List<ListItem>();

		private List<FireExtinguisherClass> listfe = new List<FireExtinguisherClass>();

		private bool isReadConfig = true;

		private Dictionary<string, string> dic;
	}
}
